<template>
  <div class="base_card" @click="_click">
    <div class="base_card_content">
      <div v-if="title" class="base_card_title">
        <div class="base_card_title_name">
          <slot name="header" />
          <span>{{ title }}</span>
        </div>
        <van-icon v-if="arrow" class="into" name="arrow" @click="into" />
      </div>
      <div v-for="(item, index) in list" :key="index" class="base_card_row">
        <span class="base_card_key">{{ item.key }}</span>
        <span
          :class="item.danger ? 'base_card_value danger' : 'base_card_value'"
          >{{ item.value }}</span
        >
      </div>
      <span v-if="list.length === 0" class="empty">{{ emptyStr }}</span>
    </div>
    <div
      v-if="collapse && data.length > collapseRow"
      class="base_card_collapse"
    >
      <span @click="toCollapse">{{ isCollapse ? '展示更多' : '收起' }}</span>
    </div>
  </div>
</template>

<script>
export default {
  name: 'baseCard',
  props: {
    // 传值类型 [{key:'', value: ''},...]
    data: {
      type: Array,
      default: () => []
    },
    title: {
      type: String,
      default: ''
    },
    // 展示右箭头
    arrow: {
      type: Boolean,
      default: false
    },
    // 是否折叠
    collapse: {
      type: Boolean,
      default: false
    },
    // 折叠行数
    collapseRow: {
      type: Number,
      default: 2
    },
    emptyStr: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      list: [],
      isCollapse: true
    }
  },
  created() {
    if (this.collapse) {
      this.list = this.data.slice(0, this.collapseRow)
    } else {
      this.list = this.data
    }
  },
  mounted() {},
  methods: {
    _click() {
      this.$emit('click')
    },
    toCollapse() {
      if (this.isCollapse) {
        this.list = this.data
      } else {
        this.list = this.data.slice(0, this.collapseRow)
      }
      this.isCollapse = !this.isCollapse
    },
    into() {
      this.$emit('into')
    }
  }
}
</script>

<style lang="scss" scoped>
.empty {
  font-family: PingFang SC Regular;
  font-size: 14px;
  font-weight: normal;
  line-height: 24px;
  letter-spacing: 0px;
  color: #656c74;
  width: 84px;
  display: inline-block;
  margin-right: 15px;
}
.base_card {
  width: 345px;
  border-radius: 6px;
  background: #fff;
  box-shadow: 0px 2px 8px 0px rgba(178, 184, 189, 0.1);
  position: relative;
  &_content {
    padding: 15px;
  }

  &_row {
    margin-bottom: 10px;
    display: flex;
    &:last-of-type {
      margin-bottom: 0;
    }
  }

  &_title {
    font-family: PingFang SC Medium;
    font-size: 16px;
    font-weight: normal;
    line-height: 22px;
    letter-spacing: 0px;
    color: #171819;
    margin-bottom: 15px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    &_name {
      width: 315px;
      text-overflow: ellipsis;
      overflow: hidden;
      word-break: break-all;
      white-space: nowrap;
      display: flex;
    }

    .into {
      font-size: 12px;
      color: #b2b8bd;
    }

    .danger {
      font-family: PingFangSCMedium-Medium;
      font-size: 12px;
      /* 错误色/错误色 */
      color: #ea0000;
      display: inline-block;
      width: 86px;
      height: 18px;
      text-align: center;
      line-height: 18px;
      /* 错误色/错误色 */
      border: 0.5px solid #ea0000;
      border-radius: 2px;
    }
  }

  &_key {
    font-family: PingFang SC Regular;
    font-size: 14px;
    font-weight: normal;
    line-height: 24px;
    letter-spacing: 0px;
    color: #656c74;
    width: 84px;
    display: inline-block;
    margin-right: 15px;
  }

  &_value {
    font-family: PingFang SC Regular;
    font-size: 14px;
    font-weight: normal;
    line-height: 24px;
    letter-spacing: 0px;
    color: #171819;
    width: 216px;

    // text-overflow: ellipsis;
    // overflow: hidden;
    word-wrap: break-word; // 字母数字换行

    &.danger {
      color: #fe3737;
    }
  }

  &_bottom {
    text-align: center;
    border-width: 0.5px 0 0 0;
    border-style: solid;
    border-color: #ebebeb;
    width: 100%;
    height: 46px;
    line-height: 46px;
    text-align: center;
    background: #fff;
    display: flex;

    span {
      display: block;
      flex: 1;
      font-family: PingFang SC Regular;
      font-size: 14px;
      font-weight: normal;
      text-align: center;
      color: #2163e0;

      &.danger {
        color: #ff5454;
      }

      &.mid_line {
        border-width: 0 0.5px 0 0;
        border-style: solid;
        border-color: #ebebeb;
      }
    }
  }

  &_collapse {
    text-align: center;
    border-width: 0.5px 0 0 0;
    border-style: solid;
    border-color: #ebebeb;
    width: 100%;
    height: 46px;
    line-height: 46px;
    background: #fff;

    span {
      font-family: PingFang SC Regular;
      font-size: 14px;
      font-weight: normal;
      line-height: 24px;
      text-align: center;
      letter-spacing: 0px;
      color: #2163e0;
    }
  }
}
</style>
